// pages/face-list/face-list.js
const api = require("../../config/api")
Page({
 /**
 * 页面的初始数据
 */
 data: {
 dataDict: {
 data:[
 {
 "id":1,
 "name":"yyy",
 "area":"#20",
 "avatar":"/images/002.jpg"
 }
 ],
 today_count:13,
 total_count:18
 }
 },
 refresh() {
 //1.发送网络请求
 //2.绑定数据
 wx.showLoading({
 title: '正在获取采集数据...',
 mask: true
 })
 wx.request({
 url: api.bank,
 method: "GET",
 success:(res) => {
 this.setData({
 dataDict: res.data
 })
 },
 complete() {
 wx.hideLoading()
 }
 })
 
 },
 deleteRow(e) {
 wx.showModal({
 title: '确认删除',
 content: '您当前正在删除采集的信息，请确认是否删除？',
 confirmColor: '#ff461f',
 complete: (res) => {
 if (res.cancel) {
 return
 } 
 if (res.confirm) {
 var nid = e.currentTarget.dataset.nid
 var index = e.currentTarget.dataset.index
 var dataList = this.data.dataDict.data
 dataList.splice(index,1)
 wx.showLoading({
 title: '删除中...',
 mask:true
 })
 wx.request({
 url: api.bank + nid + '/',
 method: 'DELETE',
 success:(res) => {
 let total_count = this.data.dataDict.total_count -1
 if(total_count < 0) {
 total_count = 0
 }
 let today_count = this.data.dataDict.today_count -1
 if(today_count < 0) {
 today_count = 0
 }
 
 this.setData({
 ["dataDict.data"]: dataList,
 ["dataDict.total_count"]: total_count,
 ["dataDict.today_count"]: today_count
 })
 
 },
 complete() {
 wx.hideLoading()
 }
 })
 }
 }
 })
 },
 bindToForm(e) {
 wx.navigateTo({
 url: '/pages/form/form'
 })
 },
addRow(row) {
 var dataList = this.data.dataDict.data
 dataList.unshift(row)
 this.setData({
 ["dataDict.data"]:dataList,
 ["dataDict.total_count"]:this.data.dataDict.total_count + 1,
 ["dataDict.today_count"]: this.data.dataDict.today_count + 1
 });
 },
 /**
 * 生命周期函数--监听页面加载
 */
 onLoad(options) {
 this.refresh();
 },
 /**
 * 生命周期函数--监听页面初次渲染完成
 */
 onReady() {
 },
 /**
 * 生命周期函数--监听页面显示
 */
 onShow() {
 },
 /**
 * 生命周期函数--监听页面隐藏
 */
 onHide() {
 },
 /**
 * 生命周期函数--监听页面卸载
 */
 onUnload() {
 },
 /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
 onPullDownRefresh() {
 this.refresh();
 },
 /**
 * 页面上拉触底事件的处理函数
 */
 onReachBottom() {
 },
 /**
 * 用户点击右上角分享
 */
 onShareAppMessage() {
 }
})